// use for Mat matrix

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
using namespace cv;

/** Function Headers */

/** Global variables */

string window_name = "Color Segmentation";
  


int split(Mat frame, Mat ch1, Mat ch2, Mat ch3)
{

	for(int i = 0; i < frame.rows; i++)
		for(int j = 0; j < frame.cols; j++)
		{
			Vec3b ele = frame.at<Vec3b>(i,j);
			//			printf("ele (%d,%d) has chan1=%d, chan2=%d, chan3=%d\n",i,j, ele[0],ele[1],ele[2]);

			ch1.at<uchar>(i,j) = ele[0];
			ch2.at<uchar>(i,j) = ele[1];
			ch3.at<uchar>(i,j) = ele[2];
		}

	return 0;
}

int CutChanFrame(Mat frame, int chan)
{

	for(int i = 0; i < frame.rows; i++)
		for(int j = 0; j < frame.cols; j++)
		{
			frame.at<Vec3b>(i,j)[chan] = 2;			
		}

	return 0;
}

/** @function main */
int main( int argc, const char** argv )
{
	CvCapture* capture;
	Mat frame = imread("cow.jpg", 1);

	
	/*
	Point2f c;
	c.x = (float)frame.rows/2;
	c.y = (float)frame.cols/2;
	Mat rot = getRotationMatrix2D( c, -45, 1);

	Mat dst;
	Size size;
	size.height = frame.rows;
	size.width = frame.cols;
	warpAffine(frame, dst, rot, size, INTER_LINEAR, BORDER_DEFAULT, 0) ;
	*/

	// matrix para
	uchar* data_ptr = frame.data;

	printf("step = %d\n",frame.step[0]);

	uchar* pij = frame.data+frame.step[0]*10+frame.step[1]*30;

	printf("Pointer (%d,%d) = %d\n", 10, 30, *pij);
	pij++;
	printf("Pointer +1 (%d,%d) = %d\n", 10, 30, *pij);
	pij++;
	printf("Pointer +2 (%d,%d) = %d\n", 10, 30, *pij);

	Vec3b ele = frame.at<Vec3b>(10,30);

	printf("ele (%d,%d) has chan1=%d, chan2=%d, chan3=%d\n",10,30, ele[0],ele[1],ele[2]);

	printf("At method (%d,%d) = %d\n", 30, 10, frame.at<uchar>(30,10));


	printf("The %d-d matrix has %d rows & %d cols & depth=%d\n", frame.dims, frame.rows, frame.cols, frame.channels());

	printf("%d\n", *data_ptr);

//	printf("(%d,%d) = %d\n", 10, 30, frame.at<uchar>(10,30));

	// EOmtx para

	// Matrix Access
	
//	p = frame.at<Point3i>(1,1);

	//Vec3b ele = frame.at<Vec3b>(1,1); 

	
	Mat ch1 = Mat(frame.rows, frame.cols, CV_8UC1);
	Mat ch2 = Mat(frame.rows, frame.cols, CV_8UC1);
	Mat ch3 = Mat(frame.rows, frame.cols, CV_8UC1);

	split(frame, ch1, ch2, ch3);
	CutChanFrame(frame,1);

	// EOMatrix Access
	
	imshow(window_name, frame);
	imshow("channel 1", ch1);
	imshow("channel 2", ch2);
	imshow("channel 3", ch3);

	waitKey();

	return 0;
}
